package com.zjy.pgcms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zjy.pgcms.common.BaseResponse;
import com.zjy.pgcms.common.Result;
import com.zjy.pgcms.dto.pet.PetAddDTO;
import com.zjy.pgcms.dto.pet.PetUpdateDTO;
import com.zjy.pgcms.entity.Pet;
import com.zjy.pgcms.entity.User;
import com.zjy.pgcms.service.PetService;
import com.zjy.pgcms.util.BaseUtil;
import com.zjy.pgcms.util.FileUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class PetController {
    @Resource
    private PetService petService;

    @Value("${staticFile.path}")
    private String staticFilePath;

    @GetMapping("/get/pet")
    public BaseResponse getPetsByUserId(HttpServletRequest request){
        User user = (User)request.getSession().getAttribute("user");
        Long userId = user.getId();
        QueryWrapper<Pet> queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        List<Pet> list = petService.list(queryWrapper);
        return Result.success(list);
    }

    @PostMapping("/pet/add")
    public BaseResponse addPet(HttpServletRequest request,PetAddDTO dto){
        Long userId = BaseUtil.getLoginUserId(request);
        Integer age = dto.getAge();
        Byte gender = dto.getGender();
        String name = dto.getName();
        String variety = dto.getVariety();
        Double weight = dto.getWeight();
        MultipartFile file = dto.getFile();

        Pet pet = new Pet(userId, name, variety, age, gender, weight);
        boolean save = petService.save(pet);
        if (save && file!=null){
            String avatarPathNet = FileUtil.saveAvatar(file, staticFilePath, pet.getId());
            pet.setAvatarUrl(avatarPathNet);
            petService.updateById(pet);
            return Result.success("");
        }
        return Result.success("");
    }


    @PostMapping("/pet/update")
    public BaseResponse updatePet(HttpServletRequest request, PetUpdateDTO dto){
        Long petId = dto.getPetId();
        Long userId = BaseUtil.getLoginUserId(request);
        Integer age = dto.getAge();
        Byte gender = dto.getGender();
        String name = dto.getName();
        String variety = dto.getVariety();
        Double weight = dto.getWeight();
        MultipartFile file = dto.getFile();
        Pet pet = new Pet(petId,userId, name, variety, age, gender, weight);
        String avatarPathNet = FileUtil.saveAvatar(file, staticFilePath, petId);
        pet.setAvatarUrl(avatarPathNet);
        petService.updateById(pet);
        return Result.success();
    }

    @DeleteMapping("/delete/pet/{id}")
    public BaseResponse deletePet(@PathVariable Long id){
        boolean b = petService.removeById(id);
        return Result.success(id);
    }

    @GetMapping("/pet/one/{id}")
    public Object getOne(@PathVariable Long id){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",id);
        Pet one = petService.getOne(queryWrapper);
        return one;
    }

    @GetMapping("/pet/get/ids")
    public BaseResponse getAllPetId(HttpServletRequest request){
        List list = petService.queryList();
        return Result.success(list);
    }







}
